//
//  NetUnConfusion.swift
//  kano
//
//  Created by Frank on 2024/9/19.
//

import Foundation
import KakaJSON

struct TrueResponse<Element>: Convertible {
    let content: Element? = nil
    let message: String = ""
    private(set) var status: Int = 0
}

struct TrueContent: Convertible, Codable {
    var authenticationToken: String = ""
    var operationalStatus: Int = 0
    var validationOutcome: Int = 0
    var creditComponent: String = ""
}

typealias TrueHandler<T> = (_ isSuccess: Bool, _ message: String, _ response: TrueResponse<T>?) -> ()

extension TrueResponse {
    //MARK: Response handle
    static func handleResponseData(_ data: Data, result:TrueHandler<Element>) {
        guard let responseString = String(data: data, encoding: .utf8) else {
            return result(false,"", nil)
        }
        var decryptString: String
        if responseString.isEmpty {
            return result(false,"The returned data is empty", nil)
        }
        if responseString.isHexString() {
            decryptString = KAEncrypt.ka_decrypt(responseString)
        } else {
            decryptString = responseString
        }
        let responseDic = dealStringToObject(with: decryptString) as! Dictionary<String, Any>
        let response = responseDic.kj.model(TrueResponse<Element>.self)
        if response.status == 0 {
            return result(true,response.message, response)
        } else {
            return result(false,response.message, response)
        }
        
    }
}


